//
//  VisitorLoginView.swift
//  SinaWeibo
//
//  Created by Yuan on 16/4/8.
//  Copyright © 2016年 Yuan. All rights reserved.
//

import UIKit


protocol VisitorLoginViewDelegate: NSObjectProtocol
{
    //协议
    
    //登录
    func visitorWillLogin()

    //注册
    func visitorWillRegister()
}


class VisitorLoginView: UIView {
    //在这里添加
    
    
    //声明代理属性
    //属性默认是强引用。需要添加weak
    weak var visitorDelegate: VisitorLoginViewDelegate?
    
    @objc func loginDidBtnCilck(){
        //代理调用协议方法
        visitorDelegate?.visitorWillLogin()
    }
    
    @objc func registerDidBtnCilck(){
        //代理调用
        visitorDelegate?.visitorWillRegister()
    }

    
    
    
     func setUIInfo(imageName: String?, title: String){
        iconView.hidden = false
        tiplabel.text = title
        if imageName != nil{
            circleView.image = UIImage(named: imageName!)
            iconView.hidden = true
            sendSubviewToBack(backView)
        }else{
            startAnimation()
        }
        
        
        
    }
    
    private func startAnimation(){
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.repeatCount = MAXFLOAT
        anim.toValue = 2 * M_PI
        anim.duration = 10
        anim.removedOnCompletion = false
        circleView.layer.addAnimation(anim, forKey: nil)
    }
    
    
    //重写构造方法
    init(){
        super.init(frame: CGRectZero)
        setupUI()
    }
    required init?(coder aDecoder: NSCoder){
        fatalError("init(code:)has not")
    }
    
    //mark:设置访客模式
    private func setupUI(){
        //backgroundColor = UIColor.redColor()
        
       
          addSubview(circleView)
      
  
          addSubview(backView)
          addSubview(iconView)
          addSubview(tiplabel)
        
       
          addSubview(loginbtn)
          addSubview(registerbtn)
       
  
        
        
        //设置fram布局失效
//        circleView.translatesAutoresizingMaskIntoConstraints = false
//        iconView.translatesAutoresizingMaskIntoConstraints = false
        for v in subviews{
            v.translatesAutoresizingMaskIntoConstraints = false
        }
        
        
        //设置布局
        //VFL
        //圆圈约束
        addConstraint(NSLayoutConstraint(item: circleView, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1.0, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: circleView, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1.0, constant: -60))
        
        
        //房子图标约束
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterX, relatedBy: .Equal, toItem: circleView, attribute: .CenterX, multiplier: 1.0, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterY, relatedBy: .Equal, toItem: circleView, attribute: .CenterY, multiplier: 1.0, constant: 0))
        
        
        //文本约束
        addConstraint(NSLayoutConstraint(item: tiplabel, attribute: .CenterX, relatedBy: .Equal, toItem: circleView, attribute: .CenterX, multiplier: 1.0, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: tiplabel, attribute: .Top, relatedBy: .Equal, toItem: circleView, attribute: .Bottom, multiplier: 1.0, constant: 16))
        //文本宽度约束
        addConstraint(NSLayoutConstraint(item: tiplabel, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 224))
        
        addConstraint(NSLayoutConstraint(item: tiplabel, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 38))
        
        //登录按钮的约束
        addConstraint(NSLayoutConstraint(item: loginbtn, attribute: .Left, relatedBy: .Equal, toItem: tiplabel, attribute: .Left, multiplier: 1.0, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: loginbtn, attribute: .Top, relatedBy: .Equal, toItem: tiplabel, attribute: .Bottom, multiplier: 1.0, constant: 16))
        
        addConstraint(NSLayoutConstraint(item: loginbtn, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100))
        
        addConstraint(NSLayoutConstraint(item: loginbtn, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 35))
        
        
        //注册按钮的约束
        addConstraint(NSLayoutConstraint(item: registerbtn, attribute: .Right, relatedBy: .Equal, toItem: tiplabel, attribute: .Right, multiplier: 1.0, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: registerbtn, attribute: .Top, relatedBy: .Equal, toItem: tiplabel, attribute: .Bottom, multiplier: 1.0, constant: 16))
        
        addConstraint(NSLayoutConstraint(item: registerbtn, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100))
        
        addConstraint(NSLayoutConstraint(item: registerbtn, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 35))
        
        //设置遮罩约束

        
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[backView]-0-|", options: [], metrics: nil, views: ["backView":backView]))
        
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[backView]-(230)-|", options: [], metrics: nil, views: ["backView":backView,"registerbtn":registerbtn]))
        
        backgroundColor = UIColor(white: 0.93, alpha: 1)
        
        
        //添加按钮点击事件
        
        loginbtn.addTarget(self, action: "loginDidBtnCilck", forControlEvents: .TouchUpInside)
        
        registerbtn.addTarget(self, action: "registerDidBtnCilck", forControlEvents: .TouchUpInside)
        
        
        
        
        
        
        
        
        
        
        
    }
    
    //mark：增加加载所有控件
    private lazy var circleView: UIImageView  = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
     private lazy var iconView: UIImageView  = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    private lazy var backView: UIImageView  = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    //文本显示
    private lazy var tiplabel: UILabel = {
        let l = UILabel()
        l.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        l.textAlignment = NSTextAlignment.Center
        //var fontsize:CGFloat = 14
        l.font = UIFont.systemFontOfSize(14)
        l.textColor = UIColor.lightGrayColor()
        l.numberOfLines = 0
        l.sizeToFit()
        return l
        
    }()
    
    //登录按钮
    private lazy var loginbtn: UIButton = {
       let btn = UIButton()
        btn.setTitle("登录", forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        btn.setTitleColor(UIColor.lightGrayColor(), forState: .Normal)
        
        return btn
    }()
    
    //注册按钮
    private lazy var registerbtn: UIButton = {
        let btn = UIButton()
        btn.setTitle("注册", forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        
        return btn
    }()
    
    
}
